package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleFreemarkService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleFreemarkServiceImpl implements ApArticleFreemarkService {
    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    @Async
    public void buildArticleFreemark(ApArticle apArticle, String content) throws IOException, TemplateException {
        if (StringUtils.isBlank(content)) return;
        //2.文章内容通过freemarker生成html文件
        StringWriter out = new StringWriter();
        Template template = configuration.getTemplate("article.ftl");

        Map<String, Object> params = new HashMap<>();
        params.put("content", JSONArray.parseArray(content));

        template.process(params, out);
        InputStream is = new ByteArrayInputStream(out.toString().getBytes());

        //3.把html文件上传到minio中
        String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", is);

        //4.修改ap_article表，保存static_url字段
        ApArticle article = new ApArticle();
        article.setId(apArticle.getId());
        article.setStaticUrl(path);
        apArticleMapper.updateById(article);

    }
}
